# -*- coding: utf-8 -*-
# standard
import os
import sys

# third
from fastapi import APIRouter
from fastapi import Request
from fastapi import Response
from fastapi import Header
from fastapi import Query as QueryParam
from fastapi import Path as PathParam
from fastapi.exceptions import HTTPException

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from functions import *
from toolbox.common_tools import INCLUDE_SCHEMA


"""
@Title:   
@File: setting_view.py
@Author: walle 2024年10日24日 10时38分57秒
@Version: 1.0.0
@Desc: 
"""


router = APIRouter()
VIEW_ARGS = dict(
    tags=['传感器设置'], 
    response_model=Resp, include_in_schema=INCLUDE_SCHEMA
)

################ 选择器 ################

@router.get("/temperature_sensor/manufacturer/selector", summary="温度传感器厂商选择器", **VIEW_ARGS)
async def temperature_sensor_manufacturer_selector_view(
    authorization: str | None = Header(None, description="请求头"),
):
    """
    aiops 那边有同样的函数，因此本接口暂时不使用
    """
    if authorization == SETTING_AUTHORIZATION:
        return Resp(data=TemperatureSensorManufacturer.get_selector())
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}")
        raise HTTPException(status_code=404)


@router.get("/electricity_sensor/manufacturer/selector", summary="电表传感器厂商选择器", **VIEW_ARGS)
async def electricity_sensor_manufacturer_selector_view(
    authorization: str | None = Header(None, description="请求头"),
):
    """
    aiops 那边有同样的函数，因此本接口暂时不使用
    """
    if authorization == SETTING_AUTHORIZATION:
        return Resp(data=ElectricitySensorManufacturer.get_selector())
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}")
        raise HTTPException(status_code=404)
    

@router.get("/third_platform/merchant/selector", summary="第三方平台商户选择器", **VIEW_ARGS)
async def tianqing_merchant_selector_view(
    sensor_category: str = QueryParam(..., description="传感器类型"),
    manufacturer: str = QueryParam(..., description="厂商"),
    authorization: str | None = Header(None, description="请求头"),
):
    if authorization == SETTING_AUTHORIZATION:
        if manufacturer == ElectricitySensorManufacturer.TIAN_QING.value:
            resp = tianqing_get_merchant_selector()
        else:
            resp = Resp(message=f"传感器类型或者厂商不匹配! sensor_category: {sensor_category}, manufacturer: {manufacturer}")
        return resp
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}")
        raise HTTPException(status_code=404)
    

@router.get("/third_platform/store/selector", summary="第三方平台门店选择器", **VIEW_ARGS)
async def tianqing_store_selector_view(
    sensor_category: str = QueryParam(..., description="传感器类型"),
    manufacturer: str = QueryParam(..., description="厂商"),
    merchant_no: str = QueryParam(..., description="商户"),
    authorization: str | None = Header(None, description="请求头"),
):
    if authorization == SETTING_AUTHORIZATION:
        if manufacturer == ElectricitySensorManufacturer.TIAN_QING.value:
            resp = tianqing_get_store_selector(merchant_no)
        else:
            resp = Resp(message=f"传感器类型或者厂商不匹配! sensor_category: {sensor_category}, manufacturer: {manufacturer}")
        return resp
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}")
        raise HTTPException(status_code=404)
    
    
@router.get("/third_platform/sensor/selector", summary="第三方平台传感器选择器", **VIEW_ARGS)
async def tianqing_sensor_selector_view(
    sensor_category: str = QueryParam(..., description="传感器类型"),
    manufacturer: str = QueryParam(..., description="厂商"),
    merchant_no: str = QueryParam(..., description="商户"),
    store_no: str = QueryParam(..., description="门店"),
    customer_id: int = QueryParam(None, description="品牌用户id"),
    store_id: int = QueryParam(None, description="门店id"),
    authorization: str | None = Header(None, description="请求头"),
):
    if authorization == SETTING_AUTHORIZATION:
        if  manufacturer == ElectricitySensorManufacturer.TIAN_QING.value:
            resp = tianqing_get_sensor_selector(sensor_category=sensor_category, 
                                                merchant_no=merchant_no, store_no=store_no, 
                                                customer_id=customer_id, store_id=store_id)
        else:
            resp = Resp(message=f"传感器类型或者厂商不匹配! sensor_category: {sensor_category}, manufacturer: {manufacturer}")
        return resp
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}")
        raise HTTPException(status_code=404)
    

@router.get("/electricity_sensor/category/selector", summary="电量传感器类型选择器", **VIEW_ARGS)
async def electricity_sensor_category_selector_view(
    authorization: str | None = Header(None, description="请求头"),
):
    if authorization == SETTING_AUTHORIZATION:
        return Resp(data=ElectricitySensorCategoryOrm.get_selector())
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}")
    
    
################ 温度传感器 ################


@router.post("/temperature_sensor/add", summary="添加温度传感器", **VIEW_ARGS)
async def add_temperature_sensor_view(
    params: AddTemperatureSensorParams,
    authorization: str | None = Header(None, description="请求头"),

):
    if authorization == SETTING_AUTHORIZATION:
        return TemperatureSensorOrm.add(params=params)
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}, params: {params.model_dump()}")
        raise HTTPException(status_code=404)
    
    
@router.post("/electricity_sensor/add", summary="添加电量传感器", **VIEW_ARGS)
async def add_electricity_sensor_view(
    params: AddElectricitySensorParams,
    authorization: str | None = Header(None, description="请求头"),

):
    if authorization == SETTING_AUTHORIZATION:
        return ElectricitySensorOrm.add(params=params)
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}, params: {params.model_dump()}")
        raise HTTPException(status_code=404)
    

@router.post("/temperature_sensor/edit", summary="编辑温度传感器", **VIEW_ARGS)
async def edit_temperature_sensor_view(
    params: EditTemperatureSensorParams,
    authorization: str | None = Header(None, description="请求头"),

):
    if authorization == SETTING_AUTHORIZATION:
        return TemperatureSensorOrm.edit(params=params)
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}, params: {params.model_dump()}")
        raise HTTPException(status_code=404)
    

@router.post("/electricity_sensor/edit", summary="编辑电量传感器", **VIEW_ARGS)
async def edit_electricity_sensor_view(
    params: EditElectricitySensorParams,
    authorization: str | None = Header(None, description="请求头"),

):
    if authorization == SETTING_AUTHORIZATION:
        return ElectricitySensorOrm.edit(params=params)
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}, params: {params.model_dump()}")
        raise HTTPException(status_code=404)
    

@router.post("/sensor/delete", summary="删除传感器", **VIEW_ARGS)
async def edit_temperature_sensor_view(
    params: CommonDeleteParams,
    sensor_category: str = QueryParam(..., description="传感器类型"),
    authorization: str | None = Header(None, description="请求头"),

):
    if authorization == SETTING_AUTHORIZATION:
        if sensor_category == SensorCategory.TEMPERATURE.value:
            cls = TemperatureSensorOrm
        elif sensor_category == SensorCategory.ELECTRICITY.value:
            cls = ElectricitySensorOrm
        else:
            logger.warning(f"传感器类型错误! sensor_category: {sensor_category}")
            raise HTTPException(status_code=434, detail="传感器类型错误")
        return cls.delete(ids=params.ids)
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}, params: {params.model_dump()}")
        raise HTTPException(status_code=404)
    

################ 电量传感器 ################


@router.post("/electricity_sensor/edit", summary="编辑电量传感器", **VIEW_ARGS)
async def edit_temperature_sensor_view(
    params: EditTemperatureSensorParams,
    authorization: str | None = Header(None, description="请求头"),

):
    if authorization == SETTING_AUTHORIZATION:
        return TemperatureSensorOrm.edit(params=params)
    else:
        logger.warning(f"身份信息缺少或者错误! authorization: {authorization}, params: {params.model_dump()}")
        raise HTTPException(status_code=404)


if __name__ == '__main__':
    pass